﻿using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using System.Web;
using Api.Common;
using AppManager.Model;
using AppManager.IBLL;
using Antlr.Runtime.Misc;
using System.Web.Mvc;

namespace AppManager.Background
{
    public class Th
    {
        #region 最好不要使用静态属性
        //static IUserService userService;
        public static Func<IUserService> UserResolve { private get; set; }
        //static IUserService UserService
        //{
        //    //get { return userService ?? (userService = UserResolve()); }
        //    get { return DependencyResolver.Current.GetService<IUserService>(); }
        //}
        #endregion

        /// <summary>
        ///     右側內容區面包屑
        /// </summary>
        public static List<string> PageHeaderCrumbs
        {
            get { return CallContext.GetData("PageHeaders") as List<string>; }
            set { CallContext.SetData("PageHeaders", value); }
        }

        /// <summary>
        ///     右侧内容区下拉菜单
        /// </summary>
        public static Dictionary<string, string> PageHeaderMenus
        {
            get { return CallContext.GetData("PageHeaderCrumbs") as Dictionary<string, string>; }
            set { CallContext.SetData("PageHeaderCrumbs", value); }
        }

        /// <summary>
        ///     这个需要数据库建立起sys_menu才能够使用，所以先不使用
        /// </summary>
        public static List<string> Sidebar
        {
            get { return CallContext.GetData("Sidebar") as List<string>; }
            set { CallContext.SetData("Sidebar", value); }
        }

        public static Sys_User User
        {
            get
            {
                var user = CallContext.GetData(typeof(Sys_User).Name) as Sys_User;
                if (user == null)
                {
                    var userIdstr = CookieHelper.GetCookie(CookieName.UserId);
                    userIdstr = DESEncrypt.Decrypt(userIdstr);

                    var userId = 0;
                    int.TryParse(userIdstr, out userId);

                    //获取注入的Service
                    var userService = DependencyResolver.Current.GetService<IUserService>();
                    user = userService.GetModel(s => s.Id == userId);
                    User = user;
                }
                return user;
            }
            set { CallContext.SetData(typeof(Sys_User).Name, value); }
        }

        public static int UserId => (User?.Id).GetValueOrDefault();

        public static bool IsLogin
        {
            get { return User != null && UserId > 0; }
        }

        /// <summary>
        ///     判断当前是否在某个地址
        /// </summary>
        /// <param name="filterContext"></param>
        /// <param name="controllerName"></param>
        /// <param name="actionName"></param>
        /// <returns></returns>
        public static bool IsInThisPage(string controllerName = "home", string actionName = "projectlist")
        {
            var name1 = HttpContext.Current.Request.RequestContext.RouteData.Values["controller"]?.ToString();
            var name2 = HttpContext.Current.Request.RequestContext.RouteData.Values["action"]?.ToString();

            if (name1.ToLower() == controllerName.ToLower() &&
                name2.ToLower() == actionName.ToLower())
            {
                return true;
            }
            return false;
        }
    }
}